package com.example.yls.demoa;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Picture;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
private static final int GET_RES = 2001;
private Button btn1;
private Handler handler;
private TextView txt_result;
private ImageView img1;
private static final String TAG = "MainActivity";
private Button btn2;
private Button btn3;
private Button btn4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         iniView();
         btn1Event();
         btn2Event();
         btn3Event("15876745586");
         btn4Event("15876745586");
    }
    private void btn4Event(final String phone) {
           btn4.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View view) {
                   String url="http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";

     RequestBody requestBody=new FormBody.Builder()
             .add("mobileCode",phone)
             .add("userID","")
             .build();
                   OkHttpClient okHttpClient=new OkHttpClient();

                   Request request=new Request.Builder()
                        .url(url)
                        .post(requestBody)
                        .build();
                Call call=okHttpClient.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.e(TAG, "onFailure: "+e.toString() );
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {

                        String txt=response.body().string();
                        Message message=handler.obtainMessage();
                        message.what=GET_RES;
                        message.obj=txt;
                        handler.sendMessage(message);
                    }
                });




               }
           });

    }

    private void btn3Event(final String phone) {
       btn3.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {

               String url="http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
               url=url.concat("?mobileCode=").concat(phone).concat("&userID=");
               OkHttpClient okHttpClient=new OkHttpClient();
               final Request request = new Request.Builder()
                       .url(url)
                       .build();
               final Call call = okHttpClient.newCall(request);
               new Thread(new Runnable() {
                   @Override
                   public void run() {
                       try {
                           Response response = call.execute();
                           String txt=response.body().string();
                           Message message=handler.obtainMessage();
                           message.what=GET_RES;
                           message.obj=txt;
                           handler.sendMessage(message);
                       } catch (IOException e) {
                           e.printStackTrace();
                       }
                   }
               }).start();

           }
       });

    }

    private void btn2Event() {
             btn2.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View view) {
                     sync();
                 }
             });

    }

    private void sync() {
        String url="http://www.baidu.com";
        OkHttpClient okHttpClient=new OkHttpClient();
        final Request request = new Request.Builder()
                .url(url)
                .build();
        final Call call = okHttpClient.newCall(request);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Response response = call.execute();
                    String txt=response.body().string();
                    Message message=handler.obtainMessage();
                    message.what=GET_RES;
                    message.obj=txt;
                    handler.sendMessage(message);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();


    }

    private void btn1Event() {
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AsyncGet();
            }
        });
    }
    private void AsyncGet() {
          String url="http://www.baidu.com";
          String imgurl="http://jsxy.gdcp.cn/UploadFile/2/2019/3/19/2019319124832881.jpg";
          OkHttpClient okHttpClient=new OkHttpClient();
        Request request=new Request.Builder().url(imgurl)
                .get().build();
        Call call=okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e(TAG, e.toString() );
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                byte[] bytes=response.body().bytes();
                /*Log.e(TAG, );*/
                Message message=handler.obtainMessage();
                message.what=GET_RES;
                message.obj=bytes;
                handler.sendMessage(message);
            }
        });



    }

    private void iniView() {
        btn3=findViewById(R.id.btn3);
        btn4=findViewById(R.id.btn4);
        img1=findViewById(R.id.img1);
               btn1=findViewById(R.id.btn1);
               btn2=findViewById(R.id.btn2);
               txt_result=findViewById(R.id.txt_result);
               handler=new Handler(new Handler.Callback() {
                   @Override
                   public boolean handleMessage(Message message) {
                       if (message.what==GET_RES){
                           String context= (String) message.obj;
                          /* Bitmap bitmap = BitmapFactory.decodeByteArray(context, 0, context.length);
                           img1.setImageBitmap(bitmap);*/
                          txt_result.setText(context);
                       }
                       return false;
                   }
               });
    }
}
